package com.poh.controller;

import com.poh.Utils.judgeQuestion;
import com.poh.pojo.*;
import com.poh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Controller
public class HomeworkController {

    @Autowired
    HomeworkService homeworkService;
    @Autowired
    ChoiceQuestionService choiceQuestionService;
    @Autowired
    AnswerQuestionService answerQuestionService;
    @Autowired
    UHScoreService uhScoreService;
    @Autowired
    CourseService courseService;
    @Autowired
    UserService userService;


    @PostMapping("/releaseHomework")
    public String releaseHomework(Homework homework, HttpSession session) {
        System.out.println(homework.getQuestionBankId());
        User user = (User) session.getAttribute("user");
        Course course = (Course) session.getAttribute("course");
        homework.setHomeworkId(UUID.randomUUID().toString());
        List<User> students = userService.getStudentByCourseId(course.getCourseId());
        for (User student : students) {
            uhScoreService.addUHScore(new UHScore(0,student.getUserId(), homework.getHomeworkId(), 0, 0, null));
        }
        homework.setHomeworkCreater(user.getUserName());
        homeworkService.releaseHomework(homework);
        return "redirect:/getHomework/" + course.getCourseId();
    }

    @GetMapping("/deleteHomework/{homeworkId}")
    public String deleteHomework(@PathVariable("homeworkId") String homeworkId, HttpSession session) {
        Course course = (Course) session.getAttribute("course");
        homeworkService.deleteHomework(homeworkId);
        return "redirect:/getHomework/" + course.getCourseId();
    }
    @GetMapping("/endHomework/{homeworkId}")
    public String endHomework(@PathVariable("homeworkId") String homeworkId, HttpSession session) {
        Course course = (Course) session.getAttribute("course");
        homeworkService.updateHomeworkStatusById(homeworkId, 0);
        return "redirect:/getHomework/" + course.getCourseId();
    }
    @GetMapping("/startHomework/{homeworkId}")
    public String startHomework(@PathVariable("homeworkId") String homeworkId, HttpSession session) {
        Course course = (Course) session.getAttribute("course");
        homeworkService.updateHomeworkStatusById(homeworkId, 1);
        return "redirect:/getHomework/" + course.getCourseId();
    }

    @GetMapping("/getStudentHomework/{courseId}")
    public String getHomework(Model model, @PathVariable("courseId") int courseId, HttpSession session) {
        Course course = courseService.getCourseByCourseId(courseId);
        session.setAttribute("course", course);
        User user = (User) session.getAttribute("user");
        List<UHScore> homeworks = uhScoreService.queryHomeworkByCourseId(courseId, user.getUserId());
        model.addAttribute("homeworks", homeworks);
        return "/homework/studentHomework";
    }

    @GetMapping("/studentHomeworkInfo/{homeworkId}")
    public String intoStudentHomework(@PathVariable("homeworkId") String homeworkId, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Homework homework = homeworkService.queryHomeworkById(homeworkId);
        List<ChoiceQuestion> choiceQuestions = choiceQuestionService.getChoiceQuestionByQuestionBankId(homework.getQuestionBankId());
        List<AnswerQuestion> answerQuestions = answerQuestionService.queryAnswerQuestionByQuestionBankId(homework.getQuestionBankId());
        UHScore uhScore = uhScoreService.queryUHScoreByHomeworkId(homeworkId, user.getUserId());
        model.addAttribute("homework", homework);
        model.addAttribute("choiceQuestions", choiceQuestions);
        model.addAttribute("answerQuestions", answerQuestions);
        model.addAttribute("choiceQuestionCount", choiceQuestions.size());
        model.addAttribute("answerQuestionCount", answerQuestions.size());
        model.addAttribute("id", uhScore.getId());
        model.addAttribute("homeworkState", uhScore.getHomeworkState());
        return "/homework/studentHomeworkInfo";
    }

    @PostMapping("/submitQuestion")
    public String submitQuestion(HttpSession session, String cqCorrect, String cqSelect,
                                 String choiceQuestionCount, String answerQuestionCount,
                                 String answerQuestionKey, String myAnswerQuestionKey,
                                 String homeworkId, int id) {
        User user = (User) session.getAttribute("user");
        Course course = (Course) session.getAttribute("course");
        Double choiceCount = Double.parseDouble(choiceQuestionCount);
        Double answerCount = Double.parseDouble(answerQuestionCount);
        DecimalFormat df = new DecimalFormat("#.0");
        Double scoreValue = Double.parseDouble(df.format(100/(choiceCount + answerCount)));
        Double score = 0.0;
        if (myAnswerQuestionKey == null) {
            score = judgeQuestion.judgeAllQuestion(cqCorrect, cqSelect, scoreValue);
        } else if (cqSelect == null) {
            score = judgeQuestion.judgeAllQuestion(answerQuestionKey, myAnswerQuestionKey, scoreValue);
        } else {
            score = judgeQuestion.judgeAllQuestion(cqCorrect+","+answerQuestionKey, cqSelect+","+myAnswerQuestionKey, scoreValue);
        }
        uhScoreService.updateHomeworkScoreByUHId(new UHScore(id, user.getUserId(), homeworkId, score, 1, null));
        System.out.println("分数" + score);
        return "redirect:/getStudentHomework/" + course.getCourseId();
    }


}

